package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskPhaseMapper;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskRolesMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskPhase;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskRoles;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskPhaseService;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskRolesService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskRolesServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskRolesMapper, TaskAutotaskRoles>
        implements TaskAutotaskRolesService {

    @Override
    public void sync(ResultEntity content){
        log.debug("Auto task roles receiver content {}", content);
        JsonNode entity = content.getEntity();

        TaskAutotaskRoles data = TaskAutotaskRoles.builder()
                .rolesId(super.getLongValue(entity.get("id")))
                .description(ObjectUtils.isEmpty(entity.get("description"))?null : entity.get("description").asText())
                .hourlyFactor(ObjectUtils.isEmpty(entity.get("hourlyFactor")) ? null : entity.get("hourlyFactor").asDouble())
                .hourlyRate(ObjectUtils.isEmpty(entity.get("hourlyRate")) ? null : entity.get("hourlyRate").asDouble())
                .isActive(ObjectUtils.isEmpty(entity.get("isActive")) || entity.get("isActive").asBoolean())
                .isExcludedFromNewContracts(ObjectUtils.isEmpty(entity.get("isExcludedFromNewContracts")) || entity.get("isExcludedFromNewContracts").asBoolean())
                .isSystemRole(ObjectUtils.isEmpty(entity.get("isSystemRole")) || entity.get("isSystemRole").asBoolean())
                .name(ObjectUtils.isEmpty(entity.get("name"))?null : entity.get("name").asText())
                .quoteItemDefaultTaxCategoryId(ObjectUtils.isEmpty(entity.get("quoteItemDefaultTaxCategoryId")) ? null : entity.get("quoteItemDefaultTaxCategoryId").asInt())
                .roleType(ObjectUtils.isEmpty(entity.get("roleType")) ? null : entity.get("roleType").asInt())
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        TaskAutotaskRoles dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskRoles>lambdaQuery()
                .eq(TaskAutotaskRoles::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskRoles::getRolesId, data.getRolesId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }


}

